﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace Noris.Tools.TraceVisualiser.Components
{
    /// <summary>
    /// Vodorovná oddělovací barevná čára s popiskem
    /// </summary>
    [Description("Vodorovná oddělovací barevná čára s popiskem")]
	internal class HLine : Control
    {
        #region Konstrukce
        public HLine()
		{
            this.Padding = new Padding(2);
			this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
            this.Paint += new PaintEventHandler(HLine_Paint);
            this.Anchor = (AnchorStyles)(AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right);
        }
        private Color _LineColorLeftTop = Color.Navy;
        private Color _LineColorLeftBottom = Color.RoyalBlue;
        private Color _LineColorRightTop = Color.Transparent;
        private Color _LineColorRightBottom = Color.Lavender;
        #endregion
        #region Paint
        void HLine_Paint(object sender, PaintEventArgs e)
		{
			string text = this.Text;
			StringFormat sf = new StringFormat(StringFormatFlags.LineLimit | StringFormatFlags.NoWrap);
			PointF textPoint = new PointF(this.Padding.Left, this.Padding.Top);
			SizeF textMax = new SizeF(this.ClientSize.Width - this.Padding.Left - this.Padding.Right, this.ClientSize.Height - this.Padding.Top - this.Padding.Bottom);
			SizeF textSize = e.Graphics.MeasureString(text, this.Font, textMax, sf);
			if (textSize.Width > 0.9F * textMax.Width)
				textSize.Width = 0.9F * textMax.Width;
			RectangleF textArea = new RectangleF(textPoint, textSize);

			int lineY = (int)(Math.Round(textArea.Bottom, 0));
			int lineX = (int)(Math.Round(textPoint.X, 0));
			int lineW = (int)(Math.Round(textMax.Width, 0));
			using (LinearGradientBrush lgb1 = new LinearGradientBrush(this.ClientRectangle, this._LineColorLeftTop, this._LineColorRightTop, LinearGradientMode.Horizontal))
			{
				Rectangle rct1 = new Rectangle(lineX, lineY, lineW, 1);
				e.Graphics.FillRectangle(lgb1, rct1);
			}
			using (LinearGradientBrush lgb2 = new LinearGradientBrush(this.ClientRectangle, this._LineColorLeftBottom, this._LineColorRightBottom, LinearGradientMode.Horizontal))
			{
				Rectangle rct2 = new Rectangle(lineX, lineY + 1, lineW, 1);
				e.Graphics.FillRectangle(lgb2, rct2);
			}

            bool drawDirect = false;
            if (drawDirect)
            {
                using (SolidBrush textBrush = new SolidBrush(this.ForeColor))
                {
                    e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                    e.Graphics.DrawString(text, this.Font, textBrush, textArea, sf);
                }
            }
            else
            {
                TextRenderer.DrawText(e.Graphics, text, this.Font, Point.Round(textArea.Location), this.ForeColor, TextFormatFlags.SingleLine);
            }
		}
        #endregion
        #region Property
        [Category("HLine")]
		public override string Text
		{
			get { return base.Text; }
			set { base.Text = value; this.Refresh(); }
		}
		/// <summary>
		/// Gets or sets the font of the text displayed by the control.
		/// </summary>
		[Description("Gets or sets the font of the text displayed by the control.  The System.Drawing.Font to apply to the text displayed by the control. The default is the value of the System.Windows.Forms.Control.DefaultFont property.")]
		[AmbientValue("")]
		[Category("HLine")]
		public override Font Font
		{
			get { return base.Font; }
			set { base.Font = value; this.Refresh(); }
		}
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public bool FontBold
		{
			get { return base.Font.Bold; }
			set
			{
				Font font = this.Font;
				this.Font = new System.Drawing.Font(font, _GetFontStyle(value, font.Italic, font.Underline, font.Strikeout));
			}
		}
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public bool FontItalic
		{
			get { return base.Font.Italic; }
			set
			{
				Font font = this.Font;
				this.Font = new System.Drawing.Font(font, _GetFontStyle(font.Bold, value, font.Underline, font.Strikeout));
			}
		}
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public float FontSize
		{
			get { return base.Font.Size; }
			set
			{
				Font font = this.Font;
				this.Font = new System.Drawing.Font(font.FontFamily, value);
			}
		}
		private FontStyle _GetFontStyle(bool bold, bool italic, bool underline, bool strikeout)
		{
			FontStyle result = FontStyle.Regular;
			if (bold)
				result = (FontStyle)(result | FontStyle.Bold);
			if (italic)
				result = (FontStyle)(result | FontStyle.Italic);
			if (underline)
				result = (FontStyle)(result | FontStyle.Underline);
			if (strikeout)
				result = (FontStyle)(result | FontStyle.Strikeout);
			return result;
		}
		/// <summary>
		/// Color of the Line, Top Left part
		/// </summary>
		[Category("HLine")]
        [DefaultValue("Color.Navy")]
		public Color LineColorLeftTop
		{
			get { return this._LineColorLeftTop; }
			set { this._LineColorLeftTop = value; this.Refresh(); }
		}
		/// <summary>
		/// Color of the Line, Bottom Left part
		/// </summary>
		[Category("HLine")]
        [DefaultValue("Color.RoyalBlue")]
        public Color LineColorLeftBottom
		{
			get { return this._LineColorLeftBottom; }
			set { this._LineColorLeftBottom = value; this.Refresh(); }
		}
		/// <summary>
		/// Color of the Line, Top Left part
		/// </summary>
		[Category("HLine")]
        [DefaultValue("Color.Transparent")]
        public Color LineColorRightTop
		{
			get { return this._LineColorRightTop; }
			set { this._LineColorRightTop = value; this.Refresh(); }
		}
		/// <summary>
		/// Color of the Line, Top Left part
		/// </summary>
		[Category("HLine")]
        [DefaultValue("Color.Lavender")]
        public Color LineColorRightBottom
		{
			get { return this._LineColorRightBottom; }
			set { this._LineColorRightBottom = value; this.Refresh(); }
		}
        #endregion
    }
}
